| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
| 2 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <meta name="generator" content="AsciiDoc 7.0.2" /> |
| 7 | <style type="text/css"> |
| 8 | /* Debug borders */ |
| 9 | p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 { |
| 10 | /* |
| 11 | border: 1px solid red; |
| 12 | */ |
| 13 | } |
| 14 | |
| 15 | body { |
| 16 | margin: 1em 5% 1em 5%; |
| 17 | } |
| 18 | |
| 19 | a { color: blue; } |
| 20 | a:visited { color: fuchsia; } |
| 21 | |
| 22 | em { |
| 23 | font-style: italic; |
| 24 | } |
| 25 | |
| 26 | strong { |
| 27 | font-weight: bold; |
| 28 | } |
| 29 | |
| 30 | tt { |
| 31 | color: navy; |
| 32 | } |
| 33 | |
| 34 | h1, h2, h3, h4, h5, h6 { |
| 35 | color: #527bbd; |
| 36 | font-family: sans-serif; |
| 37 | margin-top: 1.2em; |
| 38 | margin-bottom: 0.5em; |
| 39 | line-height: 1.3; |
| 40 | } |
| 41 | |
| 42 | h1 { |
| 43 | border-bottom: 2px solid silver; |
| 44 | } |
| 45 | h2 { |
| 46 | border-bottom: 2px solid silver; |
| 47 | padding-top: 0.5em; |
| 48 | } |
| 49 | |
| 50 | div.sectionbody { |
| 51 | font-family: serif; |
| 52 | margin-left: 0; |
| 53 | } |
| 54 | |
| 55 | hr { |
| 56 | border: 1px solid silver; |
| 57 | } |
| 58 | |
| 59 | p { |
| 60 | margin-top: 0.5em; |
| 61 | margin-bottom: 0.5em; |
| 62 | } |
| 63 | |
| 64 | pre { |
| 65 | padding: 0; |
| 66 | margin: 0; |
| 67 | } |
| 68 | |
| 69 | span#author { |
| 70 | color: #527bbd; |
| 71 | font-family: sans-serif; |
| 72 | font-weight: bold; |
| 73 | font-size: 1.2em; |
| 74 | } |
| 75 | span#email { |
| 76 | } |
| 77 | span#revision { |
| 78 | font-family: sans-serif; |
| 79 | } |
| 80 | |
| 81 | div#footer { |
| 82 | font-family: sans-serif; |
| 83 | font-size: small; |
| 84 | border-top: 2px solid silver; |
| 85 | padding-top: 0.5em; |
| 86 | margin-top: 4.0em; |
| 87 | } |
| 88 | div#footer-text { |
| 89 | float: left; |
| 90 | padding-bottom: 0.5em; |
| 91 | } |
| 92 | div#footer-badges { |
| 93 | float: right; |
| 94 | padding-bottom: 0.5em; |
| 95 | } |
| 96 | |
| 97 | div#preamble, |
| 98 | div.tableblock, div.imageblock, div.exampleblock, div.verseblock, |
| 99 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, |
| 100 | div.admonitionblock { |
| 101 | margin-right: 10%; |
| 102 | margin-top: 1.5em; |
| 103 | margin-bottom: 1.5em; |
| 104 | } |
| 105 | div.admonitionblock { |
| 106 | margin-top: 2.5em; |
| 107 | margin-bottom: 2.5em; |
| 108 | } |
| 109 | |
| 110 | div.content { /* Block element content. */ |
| 111 | padding: 0; |
| 112 | } |
| 113 | |
| 114 | /* Block element titles. */ |
| 115 | div.title, caption.title { |
| 116 | font-family: sans-serif; |
| 117 | font-weight: bold; |
| 118 | text-align: left; |
| 119 | margin-top: 1.0em; |
| 120 | margin-bottom: 0.5em; |
| 121 | } |
| 122 | div.title + * { |
| 123 | margin-top: 0; |
| 124 | } |
| 125 | |
| 126 | td div.title:first-child { |
| 127 | margin-top: 0.0em; |
| 128 | } |
| 129 | div.content div.title:first-child { |
| 130 | margin-top: 0.0em; |
| 131 | } |
| 132 | div.content + div.title { |
| 133 | margin-top: 0.0em; |
| 134 | } |
| 135 | |
| 136 | div.sidebarblock > div.content { |
| 137 | background: #ffffee; |
| 138 | border: 1px solid silver; |
| 139 | padding: 0.5em; |
| 140 | } |
| 141 | |
| 142 | div.listingblock > div.content { |
| 143 | border: 1px solid silver; |
| 144 | background: #f4f4f4; |
| 145 | padding: 0.5em; |
| 146 | } |
| 147 | |
| 148 | div.quoteblock > div.content { |
| 149 | padding-left: 2.0em; |
| 150 | } |
| 151 | div.quoteblock .attribution { |
| 152 | text-align: right; |
| 153 | } |
| 154 | |
| 155 | div.admonitionblock .icon { |
| 156 | vertical-align: top; |
| 157 | font-size: 1.1em; |
| 158 | font-weight: bold; |
| 159 | text-decoration: underline; |
| 160 | color: #527bbd; |
| 161 | padding-right: 0.5em; |
| 162 | } |
| 163 | div.admonitionblock td.content { |
| 164 | padding-left: 0.5em; |
| 165 | border-left: 2px solid silver; |
| 166 | } |
| 167 | |
| 168 | div.exampleblock > div.content { |
| 169 | border-left: 2px solid silver; |
| 170 | padding: 0.5em; |
| 171 | } |
| 172 | |
| 173 | div.verseblock div.content { |
| 174 | white-space: pre; |
| 175 | } |
| 176 | |
| 177 | div.imageblock div.content { padding-left: 0; } |
| 178 | div.imageblock img { border: 1px solid silver; } |
| 179 | span.image img { border-style: none; } |
| 180 | |
| 181 | dl { |
| 182 | margin-top: 0.8em; |
| 183 | margin-bottom: 0.8em; |
| 184 | } |
| 185 | dt { |
| 186 | margin-top: 0.5em; |
| 187 | margin-bottom: 0; |
| 188 | font-style: italic; |
| 189 | } |
| 190 | dd > *:first-child { |
| 191 | margin-top: 0; |
| 192 | } |
| 193 | |
| 194 | ul, ol { |
| 195 | list-style-position: outside; |
| 196 | } |
| 197 | ol.olist2 { |
| 198 | list-style-type: lower-alpha; |
| 199 | } |
| 200 | |
| 201 | div.tableblock > table { |
| 202 | border-color: #527bbd; |
| 203 | border-width: 3px; |
| 204 | } |
| 205 | thead { |
| 206 | font-family: sans-serif; |
| 207 | font-weight: bold; |
| 208 | } |
| 209 | tfoot { |
| 210 | font-weight: bold; |
| 211 | } |
| 212 | |
| 213 | div.hlist { |
| 214 | margin-top: 0.8em; |
| 215 | margin-bottom: 0.8em; |
| 216 | } |
| 217 | td.hlist1 { |
| 218 | vertical-align: top; |
| 219 | font-style: italic; |
| 220 | padding-right: 0.8em; |
| 221 | } |
| 222 | td.hlist2 { |
| 223 | vertical-align: top; |
| 224 | } |
| 225 | |
| 226 | @media print { |
| 227 | div#footer-badges { display: none; } |
| 228 | } |
| 229 | /* Workarounds for IE6's broken and incomplete CSS2. */ |
| 230 | |
| 231 | div.sidebar-content { |
| 232 | background: #ffffee; |
| 233 | border: 1px solid silver; |
| 234 | padding: 0.5em; |
| 235 | } |
| 236 | div.sidebar-title, div.image-title { |
| 237 | font-family: sans-serif; |
| 238 | font-weight: bold; |
| 239 | margin-top: 0.0em; |
| 240 | margin-bottom: 0.5em; |
| 241 | } |
| 242 | |
| 243 | div.listingblock div.content { |
| 244 | border: 1px solid silver; |
| 245 | background: #f4f4f4; |
| 246 | padding: 0.5em; |
| 247 | } |
| 248 | |
| 249 | div.quoteblock-content { |
| 250 | padding-left: 2.0em; |
| 251 | } |
| 252 | |
| 253 | div.exampleblock-content { |
| 254 | border-left: 2px solid silver; |
| 255 | padding-left: 0.5em; |
| 256 | } |
| 257 | </style> |
| 258 | <title>lockfile API</title> |
| 259 | </head> |
| 260 | <body> |
| 261 | <div id="header"> |
| 262 | <h1>lockfile API</h1> |
| 263 | </div> |
| 264 | <div id="preamble"> |
| 265 | <div class="sectionbody"> |
| Junio C Hamano | 5bd1d59 | 2008-01-17 02:43:10 | [diff] [blame] | 266 | <p>The lockfile API serves two purposes:</p> |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 267 | <ul> |
| 268 | <li> |
| 269 | <p> |
| Junio C Hamano | 5bd1d59 | 2008-01-17 02:43:10 | [diff] [blame] | 270 | Mutual exclusion. When we write out a new index file, first |
| 271 | we create a new file <tt>$GIT_DIR/index.lock</tt>, write the new |
| 272 | contents into it, and rename it to the final destination |
| 273 | <tt>$GIT_DIR/index</tt>. We try to create the <tt>$GIT_DIR/index.lock</tt> |
| 274 | file with O_EXCL so that we can notice and fail when somebody |
| 275 | else is already trying to update the index file. |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 276 | </p> |
| 277 | </li> |
| 278 | <li> |
| 279 | <p> |
| Junio C Hamano | 5bd1d59 | 2008-01-17 02:43:10 | [diff] [blame] | 280 | Automatic cruft removal. After we create the "lock" file, we |
| 281 | may decide to <tt>die()</tt>, and we would want to make sure that we |
| 282 | remove the file that has not been committed to its final |
| 283 | destination. This is done by remembering the lockfiles we |
| 284 | created in a linked list and cleaning them up from an |
| 285 | <tt>atexit(3)</tt> handler. Outstanding lockfiles are also removed |
| 286 | when the program dies on a signal. |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 287 | </p> |
| 288 | </li> |
| 289 | </ul> |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 290 | </div> |
| 291 | </div> |
| Junio C Hamano | 5bd1d59 | 2008-01-17 02:43:10 | [diff] [blame] | 292 | <h2>The functions</h2> |
| 293 | <div class="sectionbody"> |
| 294 | <dl> |
| 295 | <dt> |
| 296 | hold_lock_file_for_update |
| 297 | </dt> |
| 298 | <dd> |
| 299 | <p> |
| 300 | Take a pointer to <tt>struct lock_file</tt>, the filename of |
| 301 | the final destination (e.g. <tt>$GIT_DIR/index</tt>) and a flag |
| 302 | <tt>die_on_error</tt>. Attempt to create a lockfile for the |
| 303 | destination and return the file descriptor for writing |
| 304 | to the file. If <tt>die_on_error</tt> flag is true, it dies if |
| 305 | a lock is already taken for the file; otherwise it |
| 306 | returns a negative integer to the caller on failure. |
| 307 | </p> |
| 308 | </dd> |
| 309 | <dt> |
| 310 | commit_lock_file |
| 311 | </dt> |
| 312 | <dd> |
| 313 | <p> |
| 314 | Take a pointer to the <tt>struct lock_file</tt> initialized |
| 315 | with an earlier call to <tt>hold_lock_file_for_update()</tt>, |
| 316 | close the file descriptor and rename the lockfile to its |
| 317 | final destination. Returns 0 upon success, a negative |
| 318 | value on failure to close(2) or rename(2). |
| 319 | </p> |
| 320 | </dd> |
| 321 | <dt> |
| 322 | rollback_lock_file |
| 323 | </dt> |
| 324 | <dd> |
| 325 | <p> |
| 326 | Take a pointer to the <tt>struct lock_file</tt> initialized |
| 327 | with an earlier call to <tt>hold_lock_file_for_update()</tt>, |
| 328 | close the file descriptor and remove the lockfile. |
| 329 | </p> |
| 330 | </dd> |
| 331 | <dt> |
| 332 | close_lock_file |
| 333 | </dt> |
| 334 | <dd> |
| 335 | <p> |
| 336 | Take a pointer to the <tt>struct lock_file</tt> initialized |
| 337 | with an earlier call to <tt>hold_lock_file_for_update()</tt>, |
| 338 | and close the file descriptor. Returns 0 upon success, |
| 339 | a negative value on failure to close(2). |
| 340 | </p> |
| 341 | </dd> |
| 342 | </dl> |
| 343 | <p>Because the structure is used in an <tt>atexit(3)</tt> handler, its |
| 344 | storage has to stay throughout the life of the program. It |
| 345 | cannot be an auto variable allocated on the stack.</p> |
| 346 | <p>Call <tt>commit_lock_file()</tt> or <tt>rollback_lock_file()</tt> when you are |
| 347 | done writing to the file descriptor. If you do not call either |
| 348 | and simply <tt>exit(3)</tt> from the program, an <tt>atexit(3)</tt> handler |
| 349 | will close and remove the lockfile.</p> |
| 350 | <p>If you need to close the file descriptor you obtained from |
| 351 | <tt>hold_lock_file_for_update</tt> function yourself, do so by calling |
| 352 | <tt>close_lock_file()</tt>. You should never call <tt>close(2)</tt> yourself! |
| 353 | Otherwise the <tt>struct |
| 354 | lock_file</tt> structure still remembers that the file descriptor |
| 355 | needs to be closed, and a later call to <tt>commit_lock_file()</tt> or |
| 356 | <tt>rollback_lock_file()</tt> will result in duplicate calls to |
| 357 | <tt>close(2)</tt>. Worse yet, if you <tt>close(2)</tt>, open another file |
| 358 | descriptor for completely different purpose, and then call |
| 359 | <tt>commit_lock_file()</tt> or <tt>rollback_lock_file()</tt>, they may close |
| 360 | that unrelated file descriptor.</p> |
| 361 | </div> |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 362 | <div id="footer"> |
| 363 | <div id="footer-text"> |
| Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame] | 364 | Last updated 02-Jul-2008 03:02:19 UTC |
| Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 365 | </div> |
| 366 | </div> |
| 367 | </body> |
| 368 | </html> |